<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* CMD Handler
*
* This Function Processes Commands
*/

require_once "commands.class.inc";

class cmdHandler {

	/* Everything that goes into the buffer starts with a command, so if it 
	 * doesn't, we immediately toss it back */
	 public static function processCmd($user,$cmdWord,$phrase) {
	 	// Check against the Room's Command List
		if (cmdHandler::roomCmd($user,$cmdWord,$phrase)==true) { return; }
	 	// Check Against the Global Command List
	 	foreach(commands::$CMDLIST as $fullword) {
			if (preg_match("~^".$cmdWord."~i",$fullword)) {
				commands::$fullword($user,$phrase);
				return;
			}
		}
		
		$user->send($cmdWord." not understood.");
	 }
	 
	 /* ROOM COMMAND */
	 public static function roomCmd($user,$cmdWord,$phrase) {
	 	$flag = false;
	 	foreach($user->in_room->exits as $exit_) {
			if (preg_match("~^".$cmdWord."~i",$exit_->command)) {
				$flag = true;
				if($phrase=="" || stristr($exit_->commandtgt,$phrase)) {
					$user->move($exit_);
					return true;
				}
			}
		}
		if($flag) { $user->send("Go where?"); }
		
		return $flag;
	 }
	 
	  /* UTILITIES */
	 public static function splitCmd($user,$phrase) {	 
	 	$phrase = cmdHandler::replaceAbbrev($phrase);
		$wholePack = explode(" ",$phrase);
		$cmdWord = $wholePack[0];
		$phrase = substr_replace($phrase,"",0,strlen($cmdWord)+1);
		cmdHandler::processCmd($user,$cmdWord,$phrase);
	 }
	 
	 public static function splitReturnNextArg(&$phrase) {
	 	if ($phrase=="") { return ""; }
	 
	 	$wholePack = explode(" ",$phrase);
		$phrase = substr_replace($phrase,"",0,strlen($wholePack[0])+1);	
		return $wholePack[0];
	 }
	 
	 public static function replaceAbbrev($phrase) {
	 	foreach(commands::$ABBREVS as $abbrev => $word){
			if($phrase[0]==$abbrev) {
				return $word . " " . substr_replace($phrase,"",0,1);
			}
		}
		return $phrase;
	 }
	 
	 
	 /*
 	* The primary output interface for formatted output.
 	*/
	public static function act($user, $format, $arg1, $arg2, $type) {
		$he_she = array("it", "he", "she");
 		$him_her = array("it", "him", "her");
  		$his_her = array("its", "his", "her");
		
		/* $arg1 and $arg2 can be passed as objects, overriding these casts, 
		otherwise, they are checked to be non-strings manually */
		$tch = $arg2;	// Target character
		$obj1 = $arg1;
		$obj2 = $arg2;
		
		/*
		* Discard null and zero-length messages.
		*/
		if ($format == "") { return; }
		
		/* Check the right room */
		$rightroom = $user->in_room;
		
		/* Make sure there is a proper target */
		if ($type == TO_TARG) {
			if (is_null($tch)) {
				ioHandler::console ("Act: null tch with TO_TARG.");
				return;
			}
			$rightroom = $tch->in_room; // We Flip Perspectives, woah
		}
		
		foreach($user->in_room->people as $person) {
			if (!$person->is_awake()) {
				continue; }
			
			if ($type == TO_CHAR && $person != $user) {
				continue; }
			if ($type == TO_TARG && ($person != $tch || $person == $user)) { // This allows us to overload for "tell" etc.
				continue; }
			if ($type == TO_ROOM && $person == $user) {
				continue; }
			if ($type == TO_NOTTARG && ($person == $user || $person == $tch)) {
				continue; }
			
			// Here are all the '?' sequences supported by 'act':
			/* ?t: Result is arg1 interpereted as a string */
			if(strstr($format,"?t")) { $format = str_replace("?t",$arg1,$format); }
			/* ?T: Result is simply the 'to' argument interpreted as a string. */
			if(strstr($format,"?T")) { $format = str_replace("?T",$arg2,$format); }
			/* ?n: Result is the name of 'character'.  If 'character' is not visible to the target result is the string 'someone'. */
			/* (functionality coming soon), right now it is just username */
			if(strstr($format,"?n")) { $format = str_replace("?n",$user->name,$format); }
			/* ?N: Result is the name of 'to' (considered as a target).  If 'to' is not visible to the target character, result is the string 'someone'. */
			if(strstr($format,"?N")) { $format = str_replace("?N",$tch->name,$format); }
			/* ?e: Result is 'he', 'she', or 'it', depending on the sex of 'character'. */
			if(strstr($format,"?e")) { $format = str_replace("?e",$he_she[$user->sex],$format); }
			/* ?E: Result is 'he', 'she', or 'it', depending on the sex of 'to' (considered as a target). */
			if(strstr($format,"?E")) { $format = str_replace("?E",$he_she[$tch->sex],$format); }
			/* ?m: Result is 'him', 'her', or 'it', depending on the sex of 'character'. */
			if(strstr($format,"?m")) { $format = str_replace("?m",$him_her[$user->sex],$format); }
			/* ?M: Result is 'him', 'her', or 'it', depending on the sex of 'to' (considered as a target). */
			if(strstr($format,"?M")) { $format = str_replace("?M",$him_her[$tch->sex],$format); }
			/* ?s: Result is 'his', 'her', or 'its', depending on the sex of 'character'. */
			if(strstr($format,"?m")) { $format = str_replace("?s",$his_her[$user->sex],$format); }
			/* ?S: Result is 'his', 'her', or 'its', depending on the sex of 'to' (considered as a target). */
			if(strstr($format,"?M")) { $format = str_replace("?S",$his_her[$tch->sex],$format); }
			
			
			$person->send($format);
		}
	}

/*		TO BE FINISHED
	
		?p
		Result is the short description of 'obj'.  If 'obj' is invisible to the
		target character, result is the string 'something'.
	
		?P
		Result is the short description of 'vo', considered as an object.  If
		'vo' is invisible to the target character, result is the string
		'something'.
	
		?d
		Result is the first word in 'vo', considered as a string.  If 'vo' is
		NULL, result is the string 'door'.  This is meant for extracting the
		name from a door's keyword list, but may be used in general for other
		keyword lists.
	*/
	 
	 
}